package com.dmp.userLabel.factory;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.springframework.util.CollectionUtils;

import com.dmp.common.string.StringUtils;
import com.dmp.userLabel.bean.CarLabelBean;
import com.dmp.util.Constant;
import com.dmp.util.JsonMapper;

public class CarLaberImpl implements ICarLaber {
	private static final String URL_PREFIX = "http://";
	private static final Logger logger = org.slf4j.LoggerFactory.getLogger(CarLaberImpl.class);

	public static void main(String[] args) {
		CarLaberImpl carLabel = new CarLaberImpl();
		byte[] ba = new byte[]{9, 84, 105, 116, 79, 76, 103, 61, 61, 9, 9, 9, 49, 50, 50, 46, 50, 50, 56, 46, 50, 51, 51, 46, 50, 48, 57, 9, 56, 48, 9, 49, 48, 46, 57, 55, 46, 52, 57, 46, 49, 53, 49, 9, 53, 52, 54, 54, 56, 9, 9, 9, 9, 9, 9, 9, 48, 9, 49, 9, 48, 9, 50, 48, 49, 55, 48, 51, 49, 51, 49, 48, 50, 56, 48, 50, 9, 50, 48, 49, 55, 48, 51, 49, 51, 49, 48, 50, 56, 48, 50, 9, 48, 9, 49, 52, 53, 52, 9, 48, 9, 49, 9, 48, 9, 77, 111, 122, 105, 108, 108, 97, 47, 53, 46, 48, 32, 40, 87, 105, 110, 100, 111, 119, 115, 32, 78, 84, 32, 54, 46, 49, 59, 32, 87, 79, 87, 54, 52, 41, 32, 65, 112, 112, 108, 101, 87, 101, 98, 75, 105, 116, 47, 53, 51, 55, 46, 51, 54, 32, 40, 75, 72, 84, 77, 76, 44, 32, 108, 105, 107, 101, 32, 71, 101, 99, 107, 111, 41, 32, 67, 104, 114, 111, 109, 101, 47, 53, 53, 46, 48, 46, 50, 56, 56, 51, 46, 56, 55, 32, 83, 97, 102, 97, 114, 105, 47, 53, 51, 55, 46, 51, 54, 9, 47, 115, 112, 101, 99, 47, 50, 55, 49, 49, 53, 47, 9, 9, 107, 46, 97, 117, 116, 111, 104, 111, 109, 101, 46, 99, 111, 109, 46, 99, 110, 9, 107, 46, 97, 117, 116, 111, 104, 111, 109, 101, 46, 99, 111, 109, 46, 99, 110, 9, 49, 52, 48, 48, 9, 9, 49, 9, 119, 119, 119, 46, 97, 117, 116, 111, 104, 111, 109, 101, 46, 99, 111, 109, 46, 99, 110, 47, 54, 53, 55, 47, 9, 54, 9, 50, 48, 48, 9, 56, 50, 53, 9, 48, 9, 49, 9, 123, 34, 115, 101, 114, 105, 101, 115, 34, 58, 34, 55, 53, 54, 34, 44, 34, 99, 97, 114, 98, 101, 104, 97, 118, 105, 111, 114, 34, 58, 34, 52, 56, 50, 44, 52, 55, 56, 34, 44, 34, 98, 114, 97, 110, 100, 34, 58, 34, 54, 56, 52, 34, 125};
		
		System.out.println(new String(ba));
		
//		String msg = "	TitOLg==			122.228.233.209	80	10.97.49.151	54668							0	1	0	20170313102802	20170313102802	0	1454	0	1	0	Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36	/spec/27115/		k.autohome.com.cn	k.autohome.com.cn	1400		1	www.autohome.com.cn/657/	6	200	825	0	1";
//
//		String[] values = msg.split("\t");
//		String[] fieldNames = "imsi,msisdn,meid,nai,remoteipv4,remoteport,localipv4,localport,gnipaddress,snipaddress,haip,userzoneid,bsid,subnet,accesstype,protocolid,protocolidentifier,starttimestr,endtimestr,duration,inputoctets,outputoctets,inputpacket,outputpacket,useragent,wholeurl,cookie,domain,host,contentlength,contenttype,iflink,referer,method,status,delay,click,protocol_type"
//				.split(",");
//		Map<String, Object> resultMap = buildFieldValue(fieldNames, values);
//		carLabel.dealCarLabel(resultMap, true, null);
//		
//		StringBuffer data = new StringBuffer();
//		for(int i=0; i<fieldNames.length; i++){
//			if(i==0){
//				data.append(resultMap.get(fieldNames[i]).toString());
//			}else{
//				data.append("	");
//				if(resultMap.get(fieldNames[i])==null){
//				   data.append("");
//				}else{
//				   data.append(resultMap.get(fieldNames[i]).toString());
//				}
//			}
//			
//		}
//		data.append("	").append("{}");
//		String value = new String(data.toString().getBytes());
//		System.out.println("value = "+value);
	}


	@Override
	public boolean dealCarLabel(Map<String, Object> map, boolean flag, ILabelRedisDao iLabelRedisDao) {
		final String carLabelField = "wholeurl";
		final String carLabelReferField = "referer";
		final String hostField = "host";
		String redisValue = "";
		if (!CollectionUtils.isEmpty(map)) {
			String fieldNameValue = map.get(carLabelField) == null ? "" : map.get(carLabelField).toString().trim();
			String fieldNameReferValue = map.get(carLabelReferField) == null ? "" : map.get(carLabelReferField).toString().trim();
			String fieldHostValue = map.get(hostField) == null ? "" : map.get(hostField).toString().trim();
			if (StringUtils.isNotBlank(fieldNameValue) || StringUtils.isNotBlank(fieldNameReferValue)) {
				fieldNameValue = fieldNameValue.split("\\?")[0].split("#")[0];
				fieldNameReferValue = fieldNameReferValue.split("\\?")[0].split("#")[0];

				if (!fieldNameValue.startsWith(URL_PREFIX)) {

					if (fieldNameValue.startsWith("/")) {
						fieldNameValue = URL_PREFIX + fieldHostValue + fieldNameValue;
					} else {
						fieldNameValue = URL_PREFIX + "/" + fieldNameValue;
					}
				}
				if (!fieldNameReferValue.startsWith(URL_PREFIX)) {
					fieldNameReferValue = URL_PREFIX + fieldNameReferValue;
				}
				//logger.info("fieldNameValue:{}", fieldNameValue);
				//logger.info("fieldNameReferValue:{}", fieldNameReferValue);
				String urlValue = iLabelRedisDao.getCarLabelValue(fieldNameValue);
				String referValue = iLabelRedisDao.getCarLabelValue(fieldNameReferValue);
				if (StringUtils.isNotBlank(urlValue) || StringUtils.isNotBlank(referValue)) {
					// 取出url和refer中的值，如果有的话，需要去重合并
					redisValue = buildRedisValue(urlValue, referValue);
					//logger.info("urlValue:{}", urlValue);
					//logger.info("referValue:{}", referValue);
					//logger.info("redisValue:{}", redisValue);
				}

				map.put(Constant.CarLabelType.CAR_LABEL_TYPE, redisValue);
			}
		}

		return flag || StringUtils.isNotBlank(redisValue);
	}

	private String buildRedisValue(String urlValue, String referValue) {
		CarLabelBean urlBean = null;
		CarLabelBean referBean = null;
		CarLabelBean resultBean = new CarLabelBean();
		if (StringUtils.isNotBlank(urlValue)) {
			urlBean = (CarLabelBean) JsonMapper.fromJsonString(urlValue, CarLabelBean.class);
		}

		if (StringUtils.isNotBlank(referValue)) {
			referBean = (CarLabelBean) JsonMapper.fromJsonString(referValue, CarLabelBean.class);
		}

		String resultSeries = "";
		String resultCarbehavior = "";
		String resultBrand = "";

		if (urlBean == null) {
			if (referBean == null) {
				return "";
			} else {
				resultSeries = referBean.getSeries() == null ? "" : referBean.getSeries();
				resultCarbehavior = referBean.getCarbehavior() == null ? "" : referBean.getCarbehavior();
				resultBrand = referBean.getBrand() == null ? "" : referBean.getBrand();
			}
		} else {
			if (referBean == null) {
				resultSeries = urlBean.getSeries() == null ? "" : urlBean.getSeries();
				resultCarbehavior = urlBean.getCarbehavior() == null ? "" : urlBean.getCarbehavior();
				resultBrand = urlBean.getBrand() == null ? "" : urlBean.getBrand();

			} else {
				String urlSeries = urlBean.getSeries() == null ? "" : urlBean.getSeries();
				String urlCarbehavior = urlBean.getCarbehavior() == null ? "" : urlBean.getCarbehavior();
				String urlBrand = urlBean.getBrand() == null ? "" : urlBean.getBrand();

				String referSeries = referBean.getSeries() == null ? "" : referBean.getSeries();
				String referCarbehavior = referBean.getCarbehavior() == null ? "" : referBean.getCarbehavior();
				String referBrand = referBean.getBrand() == null ? "" : referBean.getBrand();

				if (urlSeries.equals(referSeries)) {
					resultSeries = urlSeries;
				} else {
					resultSeries = urlSeries + "," + referSeries;
				}
				if (urlCarbehavior.equals(referCarbehavior)) {
					resultCarbehavior = urlCarbehavior;
				} else {
					resultCarbehavior = urlCarbehavior + "," + referCarbehavior;
				}
				if (urlBrand.equals(referBrand)) {
					resultBrand = urlBrand;
				} else {
					resultBrand = urlBrand + "," + referBrand;
				}

			}
		}

		resultBean.setBrand(resultBrand);
		resultBean.setCarbehavior(resultCarbehavior);
		resultBean.setSeries(resultSeries);

		return JsonMapper.toJsonString(resultBean);
	}

}
